﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using QuickORM.BLL;

namespace QuickORM.Common
{
    public class Handler
    {
        public static void addAppsql(string id, string code, string name, string app_index, ref string result)
        {
            var insertsql = string.Concat("INSERT INTO dbo.T_Sys_AppBox ( id, code, name, app_index, UserID )VALUES  ('", id, "','", code, "','", name, "','", app_index, "',1)");
            var obj = BLLFactory<T_Sys_MenuManager>.Instance.SqlExecute(insertsql);
            if (obj > 0)
            {
                result = "+ok";
            }
        }

        public static string addScreen(string app_index)
        {
            var result = "error";
            var select = "SELECT 1 FROM t_sys_screen WHERE id='" + app_index + "' and UserID=1";
            var dt = BLLFactory<T_Sys_MenuManager>.Instance.SqlTable(select);
            if (dt.Rows.Count > 0)
            {
                result = "+ok";
            }
            else
            {
                var insertsql = "INSERT INTO T_Sys_Screen ( id,UserID )VALUES  ('" + app_index + "',1)";
                var obj = BLLFactory<T_Sys_MenuManager>.Instance.SqlExecute(insertsql);
                if (obj > 0)
                {
                    result = "+ok";
                }
            }
            return result;
        }

        public static string DelModule(string app_index)
        {
            var result = "error";
            var delsql = "DELETE FROM T_Sys_AppBox WHERE id='" + app_index + "' AND UserID=1";
            var obj = BLLFactory<T_Sys_MenuManager>.Instance.SqlExecute(delsql);
            if (obj > 0)
            {
                result = "+ok";
            }
            return result;
        }

        public static string DelScreen(string app_index)
        {
            var list = new List<string>();
            var result = "error";
            var s0 = "DELETE FROM dbo.T_Sys_Screen WHERE id='" + app_index + "' AND UserID=1";
            var s = "DELETE FROM T_Sys_AppBox WHERE app_index='" + app_index + "' AND UserID=1";
            var s2 = "UPDATE dbo.T_Sys_Screen SET id=id-1 WHERE id>'" + app_index + "' AND UserID=1";
            var s3 = "UPDATE dbo.T_Sys_AppBox SET app_index=app_index-1 WHERE app_index>'" + app_index + "' AND UserID=1";
            list.Add(s0);
            list.Add(s);
            list.Add(s2);
            list.Add(s3);
            DbTransaction trans = null;
            var r = 0;
            for (var i = 0; i < list.Count; i++)
            {
                r += BLLFactory<T_Sys_MenuManager>.Instance.SqlExecute(list[i], trans);
            }

            if (r > 0)
            {
                result = "+ok";
            }
            return result;
        }

        public static string GetAppBox()
        {
            var moduleIdStr = "'";
            var modules = "[";
            var moduleInfo = "{";
            var moduleInfoIndex = "{";
            var sql = "SELECT a.id AS app_index,b.id,b.code,b.name,b.UserID FROM t_sys_screen a LEFT JOIN dbo.T_Sys_AppBox b ON a.id =b.app_index AND a.UserID=b.UserID where b.UserID=1";
            var dt = BLLFactory<T_Sys_MenuManager>.Instance.SqlTable(sql);
            var app_index = Convert.ToInt16(dt.Compute("max(app_index)", "")) + 1;
            var strindex = new string[app_index];
            for (var i = 0; i < strindex.Length; i++)
            {
                strindex[i] = "{title: \"\",id: \"\",items:[";
            }
            foreach (DataRow row in dt.Rows)
            {
                moduleIdStr += row["code"].ToString().Trim();
                moduleIdStr += ",";
                string[] array;
                var expr_CA = array = strindex;
                IntPtr intPtr;
                var expr_E3 = (int) (intPtr = (IntPtr) int.Parse(row["app_index"].ToString()));
                var text = array[(int) intPtr];
                expr_CA[expr_E3] = string.Concat(text, "{ id: \"", row["code"].ToString().Trim(), "\", name: \"", row["name"].ToString().Trim(), "\", func_id: \"", row["id"].ToString().Trim(), "\", count: 0 },");
                text = moduleInfo;
                moduleInfo = string.Concat(text, "\"", row["code"].ToString().Trim(), "\":", row["id"].ToString().Trim(), ",");
                text = moduleInfoIndex;
                moduleInfoIndex = string.Concat(text, "\"a", row["id"].ToString().Trim(), "\": \"", row["code"].ToString().Trim(), "\",");
            }
            moduleIdStr += "'";
            for (var i = 0; i < strindex.Length; i++)
            {
                strindex[i] = strindex[i].Substring(0, strindex[i].Length - 1) + "]}";
            }
            for (var j = 0; j < strindex.Length; j++)
            {
                modules = modules + strindex[j] + ",";
            }
            modules = modules.Substring(0, modules.Length - 1) + "]";
            moduleInfo = moduleInfo.Substring(0, moduleInfo.Length - 1) + "}";
            moduleInfoIndex = moduleInfoIndex.Substring(0, moduleInfoIndex.Length - 1) + "}";
            return string.Concat(moduleIdStr, "|", modules, "|", moduleInfo, "|", moduleInfoIndex);
        }

        public static string GetData(string RoleID)
        {
            //  string sql = "SELECT CASE pid WHEN 0 THEN 'm'+CONVERT(CHAR(10),id) ELSE 'f'+CONVERT(CHAR(10),id) END  id,code,[name],url,[image] FROM dbo.T_Sys_Menu";
            var sql = "SELECT CASE pid WHEN 0 THEN 'm'+CONVERT(CHAR(10),id) ELSE 'f'+CONVERT(CHAR(10),id) END  id,code,[name],url,[image] FROM dbo.T_Sys_Menu  ";
            sql += string.Format(" WHERE id IN (SELECT FunctionID FROM  dbo.T_Function2Role WHERE RoleID='{0}')", RoleID);
            var dt = BLLFactory<T_Sys_MenuManager>.Instance.SqlTable(sql);
            var str = new StringBuilder();
            foreach (DataRow row in dt.Rows)
            {
                str.Append(row["id"].ToString().Trim().ToLower());
                str.Append(",");
                str.Append(row["name"].ToString().Trim());
                str.Append(",");
                str.Append(row["url"].ToString().Trim());
                str.Append(",");
                str.Append(row["image"].ToString().Trim());
                str.Append("|");
            }
            var cstr = "error";
            if (str.ToString().Length > 0)
            {
                cstr = str.ToString().Substring(0, str.ToString().Length - 1);
            }
            return cstr;
        }

        public static string GetMenu2(string id, string RoleID)
        {
            var str = new StringBuilder();
            var str2 = "error";
            var sql = "";
            if (id != null)
            {
                sql = "SELECT id FROM dbo.T_Sys_Menu WHERE pid='" + id + "'";
                sql += string.Format(" AND id IN (SELECT FunctionID FROM  dbo.T_Function2Role WHERE RoleID='{0}')", RoleID);
            }
            if (sql != "")
            {
                var dt = BLLFactory<T_Sys_MenuManager>.Instance.SqlTable(sql);
                foreach (DataRow row in dt.Rows)
                {
                    str.Append(row["id"].ToString().Trim());
                    str.Append(",");
                }
                if (str.ToString().Length > 0)
                {
                    str2 = str.ToString().Substring(0, str.ToString().Length - 1);
                }
            }
            return str2;
        }

        public static string GetMenuImage()
        {
            var availappicon = "['default',";
            var sql = "SELECT image FROM  dbo.T_Sys_Menu";
            var dt = BLLFactory<T_Sys_MenuManager>.Instance.SqlTable(sql);
            foreach (DataRow row in dt.Rows)
            {
                availappicon += "'";
                availappicon += row["image"].ToString().Trim();
                availappicon += "',";
            }
            availappicon += "]";
            return availappicon;
        }
    }
}